package com.czl.module_notice.viewmodel

import android.os.Bundle
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.NoticeListBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.MessageHelper
import com.czl.base.util.RxThreadHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/10 11:28
 */
class NoticeListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 0
    var isRead = "0"
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<NoticeListBean> = SingleLiveEvent()
        val navScoreEvent: SingleLiveEvent<String> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getNoticeMsgList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 0
        getNoticeMsgList()
    })

    private fun getNoticeMsgList() {
        model.getNoticeMsgList(
            mapOf(
                "areaNo" to model.getAreaId(),
                "pageNum" to (currentPage + 1).toString(),
                "pageSize" to "10",
                "isRead" to isRead,
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<NoticeListBean>>() {
                override fun onResult(t: BaseBean<NoticeListBean>) {
                    if (t.code == 200) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }

    fun readNotice(noticeBean: NoticeListBean.Data) {
        if (noticeBean.isRead == 0) {
            model.readNotice(noticeBean.id)
                .compose(RxThreadHelper.rxSchedulerHelper(this))
                .doOnSubscribe { showLoading() }
                .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                    override fun onResult(t: BaseBean<Any?>) {
                        dismissLoading()
                        if (t.code == 200) {
                            MessageHelper.navPage(noticeBean.jumpLink, this@NoticeListViewModel)
                            LiveBusCenter.postRefreshNoticeMsgEvent()
                        } else {
                            showSuccessToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        dismissLoading()
                        showErrorToast(msg)
                    }

                })
        } else {
            MessageHelper.navPage(noticeBean.jumpLink, this@NoticeListViewModel)
        }
    }


}